﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Framework;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Syndication;
using NLBS.Data;


namespace NLBS.Views
{
    public partial class PostArticlePresenter : Presenter<IPostArticleView>
    {
       public PostArticlePresenter() {
           BlogInfo = BlogInfo.Instance();
           Power = BlogInfo.CurrentUser.Group.Power;
       }
       public event CallError ErrorHander;
       private Right Power;
       private BlogInfo BlogInfo;
       public override void OnViewLoaded()
       {
           base.OnViewLoaded();
           this.ErrorHander += new CallError(View.ShowErrorMsg);
           BasicCheck();
           
           View.PostArticle += new EventHandler(View_PostArticle);
           View.EditArticle += new EventHandler(View_EditArticle);
           View.DeleteArticle += new EventHandler(View_DeleteArticle);
           View.CheckActionError += new EventHandler(View_CheckActionError);
           View.InitArticle += new EventHandler(View_InitArticle);
       }

       void View_InitArticle(object sender, EventArgs e)
       {
           GetNewOrEditArticle();
       }

       void View_CheckActionError(object sender, EventArgs e)
       {
           switch (View.act)
           {
               case "edit":
                   if (Power.Edit < 1)
                   {
                       View.ErrorMsg = "没有编辑的权限";
                       return;
                   }
                   this.View.currentArticle = ArticleData.GetArticleByID(View.articleID);
                   if (this.View.currentArticle == null)
                   {
                       View.ErrorMsg = "日志不存在";
                       return;
                   }
                   if (BlogInfo.CurrentUser.UserID != this.View.currentArticle.AuthorID && Power.Edit < 2)
                   {
                       View.ErrorMsg = "没有编辑的权限";
                       return;
                   }
                   break;
               case "delete":
                   if (Power.Delete < 1)
                   {
                       View.ErrorMsg = "没有删除的权限";
                       return;
                   }
                   View_DeleteArticle(null, null);
                   break;
           }
       }

       void View_EditArticle(object sender, EventArgs e)
       {
           View.currentArticle.EditMark = BlogInfo.CurrentUser.UserName + "$|$" + DateTime.Now.ToString();
           ArticleData.UpdateArticle(View.currentArticle);
           SaveTag();
       }

       void View_DeleteArticle(object sender, EventArgs e)
       {
           //判断日志是否为编辑者自身的日志或者拥有编辑的权限
           if (BlogInfo.CurrentUser.UserID != ArticleData.GetArticleByID(View.articleID).AuthorID && Power.Delete < 2)
           {
               View.ErrorMsg = "没有删除的权限";
               return;
           }
           ArticleData.DeleteArticle(View.articleID);
       }

       void View_PostArticle(object sender, EventArgs e)
       {
           Article article = View.currentArticle;
           article.PostTime = DateTime.Now;
           article.AuthorID = BlogInfo.CurrentUser.UserID;
           article.Author = BlogInfo.CurrentUser.UserName;
           article.TrackBackCount = 0;
           article.ViewCount = 0;
           article.CommentCount = 0;
           article.ArticleID=  ArticleData.InsertArticle(article);
           SaveTag();
       }

       private void SaveTag()
       {

           if (View.currentArticle.Tags != null && View.currentArticle.Tags.Count > 0)
           {
               DbProvider.Instance().SaveTag(View.currentArticle);
               ArticleData.GetArticleByID(View.currentArticle.ArticleID).Tags = View.currentArticle.Tags;
           }
           
       }

       private void GetNewOrEditArticle()
       {
           if (View.act == "edit")
           {
               View.currentArticle = ArticleData.GetArticleByID(View.articleID);
           }
           else
           {
               View.currentArticle = new Article();
           }
           
       }

       private void CheckBack()
       {
           View.enableTrackBack = !String.IsNullOrEmpty(View.currentArticle.TrackbackURL)
                           && BlogInfo.EnableTrackbackOut == 1 && View.currentArticle.Mode == 1;
       }

       private void BasicCheck()
       {
           //判断是否登录
           if (!HttpContext.Current.Request.IsAuthenticated)
           {
               View.ErrorMsg = "没有登陆,请登陆";
               ErrorHander(View.ErrorMsg);
               return;
           }
           //判断是否有权限
           if (Power.Post < 2)
           {
               View.ErrorMsg = "没有发布的权限";
               
               ErrorHander(View.ErrorMsg);
               return;
           }
           //View.ShowErrorMsg(View.ErrorMsg);
       }

       //private void SendPing()
       //{
       //    TrackBackWriter tb = new TrackBackWriter();
       //    string tburl = Request.Form["log_trackbackurl"];
       //    if (!String.IsNullOrEmpty(tburl))
       //    {
       //        tb.SendPing(Request.Form["log_trackbackurl"], "http://" + Request.Url.Host + Request.Url.AbsolutePath + "?id=1",
       //            "nlbs", log_title.Value, "abc");
       //    }

       //}
    }
}
